package com.linhun.nickmao.demo.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.linhun.nickmao.demo.R;
import com.linhun.nickmao.demo.viewpagerindicator.TabPageIndicator;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by NickMao on 2015/4/6.
 */
public abstract class BaseTabActivity extends BaseActivity {

    private TabPageIndicator mTabs;
    private ViewPager mTabContent;

    /**
     * 初始化Tab视图容器
     */
    private List<TabViewFragment> mTabViews = new ArrayList<TabViewFragment>();


    /**
     * 添加视图元素
     * @param tvf 视图数组
     */
    protected  void  addTabView(TabViewFragment... tvf){
        for(int i = 0;i< tvf.length;i++){
            mTabViews.add(tvf[i]);
        }
    }

    /**
     * 初始化每个Tab页的视图
     */
    protected abstract void initTabView();

    private class TabViewAdapter extends FragmentPagerAdapter{

        public TabViewAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return  mTabViews.get(i);
        }

        @Override
        public int getCount() {
            return mTabViews.size();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.Theme_CustomViewPagerPagerIndicator);
        initTabView();
        setTheme(R.style.Widget_TabPageIndicator);
        setContentView(R.layout.layout_tabs);
        mTabs = (TabPageIndicator)findViewById(R.id.tabs);
        mTabContent = (ViewPager)findViewById(R.id.tab_content);
        //设置ViewPager控件一次缓存所有的View
        mTabContent.setOffscreenPageLimit(mTabViews.size());
        mTabContent.setAdapter(new TabViewAdapter(getSupportFragmentManager()));
    }
}
